//
//  URLTest_ViewController.m
//  EpointFrame5
//
//  Created by AndLi on 15/5/12.
//  Copyright (c) 2015年 Epoint. All rights reserved.
//

#import "URLTest_ViewController.h"
#import "EpointBrowerViewController.h"

@interface URLTest_ViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *urlTextView;
@property (weak, nonatomic) IBOutlet UIButton *goButton;

@end

@implementation URLTest_ViewController

-(void)viewDidAppear:(BOOL)animated{
    if ([DBFrameService getConfigValue:Epoint_User_LoginId]!=nil && ![[DBFrameService getConfigValue:Epoint_User_LoginId] isEqualToString:@""]){
        [_urlTextView setText:[DBFrameService getConfigValue:Epoint_User_LoginId]];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"测试URL";
    
    _urlTextView.layer.borderColor = [[UIColor blackColor]CGColor];
    _urlTextView.layer.borderWidth = 1.0;
    _urlTextView.layer.masksToBounds = YES;
    
    _urlTextView.text = @"请输入URL地址";
    _urlTextView.textColor = [UIColor grayColor];
    
   
    [self addCloseButton];
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if([_urlTextView.text isEqual:@"请输入URL地址"]){
        _urlTextView.text = @"";

    }
    _urlTextView.textColor = [UIColor blackColor];
    return YES;
}

-(IBAction)clickGoButton:(id)sender{
    if (_urlTextView.text == nil || [_urlTextView.text isEqualToString:@""]|| [_urlTextView.text isEqualToString:@"请输入URL地址"]) {
        [self toastMessage:@"URL地址不能为空!"];
        return;
    }
    [DBFrameService setConfigValue:Epoint_User_LoginId value:_urlTextView.text];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:_urlTextView.text forKey:@"url"];
    [params setObject:@"测试URL"         forKey:@"title"];
    EpointBrowerViewController *vc = [[EpointBrowerViewController alloc]init];
    vc.passParam = params;
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)addCloseButton{
    //定义一个toolBar
    UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    
    //设置style
    [topView setBarStyle:UIBarStyleBlack];
    
    //义两个flexibleSpace的button，放在toolBar上，这样完成按钮就会在最右边
    UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:
                                UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:
                                 UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    //定义完成按钮
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone
                                                                  target:self action:@selector(closeKeyBoard)];
    
    //在toolBar上加上这些按钮
    NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
    [topView setItems:buttonsArray];
    
    [_urlTextView setInputAccessoryView:topView];
    
}

-(void)closeKeyBoard{
    [_urlTextView resignFirstResponder];
}

-(IBAction)clickClearButton:(id)sender{
    _urlTextView.text = @"";
    [DBFrameService setConfigValue:Epoint_User_LoginId value:@""];
}


@end
